package com.dongdongshop.shiro;


import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

public class UserRealm extends AuthorizingRealm {
    //执行授权逻辑
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("执行授权逻辑");
        //给资源进行授权
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        //添加资源的授权字符串
        info.addStringPermission("add");
        return info;
    }

    //执行认证逻辑
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("执行认证逻辑");
        //假设数据库的用户名和密码
        String user = "admin";
        String pwd = "7b18f9e91f009c2473b5ed3513549c08";
        //编写shiro判断逻辑判断用户名和密码
        //1.判断用户名
        UsernamePasswordToken token = (UsernamePasswordToken)authenticationToken;
        if (user == null){

        /*    * 用户名不存在
            * return null值,shiro底层会抛出UnknownAccountException异常
            **/
            return null;
        }
        //2,判断密码
        //1,返回值 2,查询的密码 3,盐 4,用户真实姓名

        return new SimpleAuthenticationInfo("", pwd, ByteSource.Util.bytes("admin"),"");

    }
}




























